package com.example.studentknowledgesys.controller;

import com.example.studentknowledgesys.entity.PointRecord;
import com.example.studentknowledgesys.entity.UserPoint;
import com.example.studentknowledgesys.service.PointService;
import com.example.studentknowledgesys.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user-points")
public class UserPointController {

    private final PointService pointService;

    @Autowired
    public UserPointController(PointService pointService) {
        this.pointService = pointService;
    }

    // 获取用户积分
    @GetMapping("/{userId}")
    public ResponseEntity<UserPoint> getUserPoint(@PathVariable Long userId) {
        UserPoint userPoint = pointService.getUserPoint(userId);
        if (userPoint != null) {
            return ResponseEntity.ok(userPoint);
        }
        return ResponseEntity.notFound().build();
    }

    // 获取积分排行榜
    @GetMapping("/leaderboard")
    public ResponseEntity<List<UserPoint>> getTopUsers(
            @RequestParam(defaultValue = "10") int limit) {
        List<UserPoint> topUsers = pointService.getTopUsers(limit);
        return ResponseEntity.ok(topUsers);
    }
    @GetMapping
    public Result getAllUserPoints() {
        List<UserPoint> records = pointService.getAllUserPoints();
        return Result.success(records);
    }
}